# -*- coding: utf-8 -*-  

import json,sys,os,random
import httplib
import MySQLdb
class KLineData:
  start=0
  end=0
  top=0
  low=0
  yend=0
  date=""
  def __init__(self,d,s,e,t,l,y):
    self.date=d
    self.start=s
    self.end=e
    self.top=t
    self.low=l
    self.yend=y
  def isred(self):
    return self.end - self.start >= 0
  def startwave(self):
    return (self.start-self.yend)/self.yend*100;
  def topwave(self):
    return (self.top-self.yend)/self.yend*100;
  def lowwave(self):
    return (self.low-self.yend)/self.yend*100;
  def endwave(self):
    return (self.end-self.yend)/self.yend*100;
  def log(self):
    print "@@@", self.date,self.start,self.end,self.top,self.low,self.yend

class KLine:
  lines=list()
  mline=list()
  stock=""
  def __init__(self,st):
    self.lines = list()
    self.stock=st
    flag="SH"
    if st[0] != '6':
      flag="SZ" 
    if not os.path.isfile("data/%s"%st):   
      print "download"
      os.system("wget -q \"http://hqdata.compass.cn/test/kline.py/data.znzDo?cmd=%sHQ%s|2012,|0.8741330769844353&from=qq.compass.cn\" -O data/%s" %(flag, st, st))
    f=open("data/%s"%(st))
    data=f.read()
    f.close()
    #print data
    data=data[:-1]
    if len(data)<100:
      #print "err ",st
      return
    jo=json.loads(data)
    try:
      jo = json.loads(jo[0][1])
    except:
      print "err"
    i=0
    ye=jo[0][1]
    while i<len(jo):
      #print jo[i]
      kl=KLineData(jo[i][0], jo[i][1], jo[i][4], jo[i][2], jo[i][3], ye)
      ye=jo[i][4]
      self.lines.append(kl)
      i+=1;
    self.getmline()
  def getmline(self):
    i=0
    while i<len(self.lines):
      j=0;sum=0.0
      m5=0.0;m10=0.0;m20=0.0;m30=0.0
      while j<30 and i-j>=0:
        if j==5:
          m5=sum/5
        if j==10:
          m10=sum/10
        if j==20:
          m20=sum/20
        if j==30:
          m30=sum/30 
        sum += self.lines[i-j].end
        j+=1
      if m5==0:
        m5=sum/j
      if m10==0:
        m10=sum/j
      if m20==0:
        m20=sum/j
      if m30==0:
        m30=sum/j
      self.mline.append((self.lines[i].date,m5,m10,m20,m30))
      i+=1
    #print "end"
class StockManager:
  stocks=dict()
  names=dict()
  def __init__(self):
    pass
  def getKline(self, st):
    kline=None
    if st not in self.stocks:
      kline = KLine(st)
    else:
      kline = self.stocks[st]
    if len(kline.lines)==0:
      return None
    return kline
  def checkUp10(self, sts, begin):
    res=list()
    for st in sts:
      kline=self.getKline(st)
      if not kline:
        continue
      if len(kline.lines) < begin:
        continue
      #print st, kline.lines[-begin].endwave()
      if kline.lines[-begin].endwave() > 9.95: 
        print st, kline.lines[-begin].date, kline.lines[-begin].endwave()#,kline.lines[-begin+1].startwave(),kline.lines[-begin+1].endwave()
        res.append(st) 
    return res
def getStocks():  
  sts=list()
  f=open("stocks")
  lines=f.readlines()
  for i in lines:
    tmp = i.strip().split()
    sts.append(tmp[0])
  return sts
stockmanager=StockManager()
res=stockmanager.checkUp10(getStocks(), 2)
for st in res:
  conn=MySQLdb.connect(host="10.241.84.65",user="root",db="stock")
  cursor = conn.cursor()
  sql = "insert into stock_class values('%s','%s','%s')" % ("yestoday2", "前天涨停股", st);
  try:
    cursor.execute(sql)
  except:
    pass
